package com.zyh.service;
import com.zyh.type.CodeStatusEnum;
import com.zyh.dao.CommentDao;
import com.zyh.entity.Comment;
import com.zyh.exception.BusinessException;
import com.zyh.resp.Result;
import com.zyh.utils.TransactionManager;
import java.sql.SQLException;
import java.util.List;

public class CommentService {

    private final CommentDao commentDao = new CommentDao();

    public Result<Comment> selectById(Integer commentId) {
        if (commentId == null || commentId <= 0) {
            throw new BusinessException(CodeStatusEnum.PARAM_ERROR);
        }
        try {
            TransactionManager.beginTransaction();
            Comment comment = commentDao.selectById(commentId);
            if (comment == null) {
                throw new BusinessException(CodeStatusEnum.COMMENT_NOT_FOUND);
            }
            TransactionManager.commit();
            return Result.success(comment);
        } catch (SQLException e) {
            try { TransactionManager.rollback(); } catch (SQLException ex) {}
            throw new BusinessException(CodeStatusEnum.SYSTEM_ERROR);
        } finally {
            try { TransactionManager.release(); } catch (SQLException e) {}
        }
    }

    public Result<Void> add(Comment comment) {
        if (comment.getUserId() == null || comment.getContent() == null || comment.getParentId() == null) {
            throw new BusinessException(CodeStatusEnum.PARAM_EMPTY);
        }
        try {
            TransactionManager.beginTransaction();
            commentDao.add(comment);
            TransactionManager.commit();
            return Result.success();
        } catch (SQLException e) {
            try { TransactionManager.rollback(); } catch (SQLException ex) {}
            throw new BusinessException(CodeStatusEnum.SYSTEM_ERROR);
        } finally {
            try { TransactionManager.release(); } catch (SQLException e) {}
        }
    }

    public Result<Void> likeById(Integer commentId) {
        if (commentId == null || commentId <= 0) {
            throw new BusinessException(CodeStatusEnum.PARAM_ERROR);
        }
        try {
            TransactionManager.beginTransaction();
            if (commentDao.selectById(commentId) == null) {
                throw new BusinessException(CodeStatusEnum.COMMENT_NOT_FOUND);
            }
            commentDao.likeById(commentId);
            TransactionManager.commit();
            return Result.success();
        } catch (SQLException e) {
            try { TransactionManager.rollback(); } catch (SQLException ex) {}
            throw new BusinessException(CodeStatusEnum.SYSTEM_ERROR);
        } finally {
            try { TransactionManager.release(); } catch (SQLException e) {}
        }
    }

    public Result<List<Comment>> getAll() {
        try {
            TransactionManager.beginTransaction();
            List<Comment> comments = commentDao.selectAll();
            TransactionManager.commit();
            return Result.success(comments);
        } catch (SQLException e) {
            try { TransactionManager.rollback(); } catch (SQLException ex) {}
            throw new BusinessException(CodeStatusEnum.SYSTEM_ERROR);
        } finally {
            try { TransactionManager.release(); } catch (SQLException e) {}
        }
    }

    public Result<Void> updateById(Integer commentId, String newContent) {
        if (commentId == null || commentId <= 0) {
            throw new BusinessException(CodeStatusEnum.PARAM_ERROR);
        }
        if (newContent == null || newContent.trim().isEmpty()) {
            throw new BusinessException(CodeStatusEnum.PARAM_EMPTY);
        }
        try {
            TransactionManager.beginTransaction();
            Comment comment = commentDao.selectById(commentId);
            if (comment == null) {
                throw new BusinessException(CodeStatusEnum.COMMENT_NOT_FOUND);
            }
            commentDao.updateById(commentId, newContent);
            TransactionManager.commit();
            return Result.success();
        } catch (SQLException e) {
            try {
                TransactionManager.rollback();
            } catch (SQLException ex) {
                System.err.println("回滚事务失败: " + ex.getMessage());
            }
            throw new BusinessException(CodeStatusEnum.SYSTEM_ERROR);
        } finally {
            try {
                TransactionManager.release();
            } catch (SQLException e) {
                System.err.println("释放数据库连接失败: " + e.getMessage());
            }
        }
    }
}
